#! /bin/sh
#
# Bundle maker script for Croquant (Integration between Crunchy and MoinMoin)
# This script help to make a bundle of Crunchy, MoinMoin and Croquant
# Croquant project: <http://code.google.com/p/croquant/>
#
# Copyright (c) 2008 Florian Birée <florian.biree@gmail.com>
#
# All rights reserved, see LICENSE for details.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

usage="Usage: $0 crunchy_dir moinmoin_dir dest_dir"

# Check arguments
if [ $# -ne 3 ]
then
    echo "Bad number of arguments" >&2
    echo "$usage" >&2
    exit 1
fi

crunchy_dir="$1"
moinmoin_dir="$2"
dest_dir="$3"
croquant_dir=`dirname "$0"`/..

if [ ! -d "$dest_dir" -o ! -r "$dest_dir" -o ! -w "$dest_dir" -o \
     ! -x "$dest_dir" ]
then
    echo "Check you can write in $dest_dir" >&2
    echo "$usage" >&2
    exit 2
fi

if [ ! -d "$crunchy_dir" -o ! -r "$crunchy_dir" -o ! -x "$crunchy_dir" -o \
     ! -f "$crunchy_dir"/crunchy.py ]
then
    echo "Check you can read in $crunchy_dir and that this is a Crunchy installation" >&2
    echo "$usage" >&2
    exit 3
fi

if [ ! -d "$moinmoin_dir" -o ! -r "$moinmoin_dir" -o ! -x "$moinmoin_dir" -o \
     ! -d "$moinmoin_dir"/MoinMoin ]
then
    echo "Check you can read in $moinmoin_dir and that this is a MoinMoin installation" >&2
    echo "$usage" >&2
    exit 4
fi

# Make the bundle
content=`ls "$dest_dir"`
if [ "$content" ]
then
    # $dest_dir isn't empty: we make a 'croquant-bundle' directory
    dest_dir="$dest_dir/croquant-bundle"
fi
mkdir -p "$dest_dir"
cp "$croquant_dir"/bundle/start_bundle.py  "$dest_dir"

# Copy Crunchy and MoinMoin
cp -R "$crunchy_dir" "$dest_dir"
cp -R "$moinmoin_dir" "$dest_dir"

# Install Croquant stuff in the MoinMoin wiki
sh "$croquant_dir"/setup.sh "$dest_dir"/`basename "$moinmoin_dir"`/wiki

# Copy the croquant config for MoinMoin
cp "$croquant_dir"/style/wikiconfig.py "$dest_dir"/`basename "$moinmoin_dir"`/wikiconfig_local.py

# The end
echo "The bundle is done. You can find it in: $dest_dir"
exit 0
